package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 异常车辆上报
 *
 * @Author: chen
 * @Description: 异常车辆上报
 * @Date: 创建于 16:22 2020/1/3
 */
@Controller
@RequestMapping("/machineFault")
public class MachineFaultController {
    @Autowired
    private MachineFaultService machineFaultService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountService accountService;

    /**
     * 添加故障车辆
     *
     * @param token        令牌
     * @param files        文件
     * @param userCode     车辆编号
     * @param machineFault 故障详情
     * @param lo           经度
     * @param la           纬度
     * @param mapType      地图类型
     * @param response     响应
     * @ignoreParams request
     */
    @RequestMapping("/add")
    public void add(String token, MultipartFile[] files, String userCode, MachineFault machineFault, Double lo, Double la, @RequestParam(defaultValue = "2") Integer mapType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = tokenService.getUser(token);
        try {
            Assert.notNull(machineFault.getFaults(), "-80001", "异常类型不能为空");
            Assert.notNull(userCode, "-30006", "用户码不能为空");
            Machine machine = machineService.getByUserCode(userCode);
            if (machine == null) {
                throw new BaseException("-30005", "设备不存在");
            }
            Point point = null;
            if (mapType != null && lo != null && la != null) {
                point = new Point(lo, la);
                point = calibrateService.calibrate(point, mapType, SysConstant.ORIGINAL);
            } else {
                /**未传递赋予默认值*/
                point = new Point(0D, 0D);
            }
            /**非品牌旗下设备，无权操作*/
            Account account = accountService.getByAdAccountId(machine.getAccountId());
            if (account != null && !account.getParentId().equals(user.getAccountId())) {
                throw new BaseException("-30005", "设备不存在");
            }
            /**数据组装*/
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(point.getX());
            stringBuffer.append(",");
            stringBuffer.append(point.getY());

            machineFault.setPoint(stringBuffer.toString());
            machineFault.setAddMan(user.getPhone());
            machineFault.setMachineId(machine.getMachineId());
            machineFault.setFrom(1);
            machineFaultService.insert(files, machineFault, request, user, machine);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
